<template>
  <el-dialog
    class="classifyChange"
    :title="pageLevel=='0'?(pageType=='1'?'修改':'新增')+' 一级分类':(pageType=='1'?'修改':'新增')+' 二级分类'"
    :visible.sync="visible"
    width="500px">
    <el-form class="form" label-position="top" label-width="80px">
      <el-form-item label="分类名称">
        <el-input size="small" v-model="dataForm.dictName"></el-input>
      </el-form-item>
      <el-form-item label="分类图片" v-if="pageLevel=='1'">
        <el-upload
          class="avatar-uploader"
          :action="uploadUrl"
          :headers="headers"
          :show-file-list="false"
          accept=".jpeg,.png,.jpg,.gif"
          :on-success="successPictureHandle">
          <img v-if="dataForm.pictureUrl" :src="dataForm.pictureUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
    </el-form>
    <div class="btnBox">
      <div class="btn2" @click="cancel">取消</div>
      <div class="btn1" @click="save">保存</div>
    </div>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      uploadUrl: this.$http.adornUrl('/uploadfile'),
      headers: { token: this.$cookie.get('token') },
      visible: false,
      pageType: '', // 新增还是修改 0新增 1修改
      pageLevel: '', // 一级还是二级 0一级 1二级
      dataForm: {
        pid: '',
        id: '',
        dictName: '',
        pictureUrl: ''
      }
    }
  },
  methods: {
    // 显示组件
    show (pageType, pageLevel, row, rows) {
      this.clear()
      this.visible = true
      console.log(pageType, pageLevel, row, rows)
      this.pageType = pageType
      this.pageLevel = pageLevel
      if (this.pageLevel === '0') { // 如果是一级
        this.dataForm.pid = '0'
        if (this.pageType === '1') { // 如果是修改
          this.dataForm.id = row.id
          this.dataForm.dictName = row.dictName
        }
      } else { // 如果是二级
        this.dataForm.pid = row.id
        if (this.pageType === '1') { // 如果是修改
          this.dataForm.id = rows.id
          this.dataForm.dictName = rows.dictName
          this.dataForm.pictureUrl = rows.pictureUrl
        }
      }
    },
    // 图片上传成功
    successPictureHandle (response, file, fileList) {
      console.log(response, file, fileList)
      this.dataForm.pictureUrl = response.pictureInfoVOS[0].imgAddress
    },
    // 取消
    cancel () {
      this.visible = false
      this.clear()
    },
    // 清空组件中的数据
    clear () {
      this.dataForm = {
        pid: '',
        id: '',
        dictName: '',
        pictureUrl: ''
      }
    },
    // 添加和修改
    save () {
      if (this.dataForm.dictName === '') {
        return this.$message.error('分类名称不能为空!')
      }
      let postUrl = ''
      if (this.pageType === '0') {
        postUrl = '/clothingtype/add'
      } else {
        postUrl = '/clothingtype/change'
      }
      this.$http({
        url: this.$http.adornUrl(postUrl),
        method: 'post',
        data: this.$http.adornData({
          ...this.dataForm
        })
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.$message.success('操作成功！')
          this.visible = false
          this.$emit('refreshDataList')
        } else {
          this.$message.error(data.msg)
        }
      })
    }
  }
}
</script>

<style lang="scss">
.classifyChange{
  box-shadow: 0px 0px 7px rgba(0, 0, 0, 0.12);
  border-radius: 8px 0px 0px 8px;
  .btnBox{
    display: flex;
    justify-content: space-around;
    .btn1,.btn2{
      width: 158px;
      height: 29px;
      line-height: 29px;
      text-align: center;
      background: #0085CC;
      border-radius: 5px;
      font-size: 14px;
      font-family: Arial;
      font-weight: 400;
      color: #FFFFFF;
      cursor: pointer;
    }
    .btn2{
      background: #333744;
    }
  }
}
</style>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>